package org.example.reemploymentsys.controller;

import org.example.reemploymentsys.entity.Exam;
import org.example.reemploymentsys.entity.ExamQuestion;
import org.example.reemploymentsys.entity.Organization;
import org.example.reemploymentsys.pojo.ExamData;
import org.example.reemploymentsys.repository.ExamRe;
import org.example.reemploymentsys.repository.OrganizationRe;
import org.example.reemploymentsys.repository.UserTbRe;
import org.example.reemploymentsys.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ExamPaperController {
    @Autowired
    private UserTbRe userTbRe;
    @Autowired
    private ExamRe examRe;
    @Autowired
    private OrganizationRe organizationRe;
    @Autowired
    private ExamService examService;
    @PostMapping("/getExamPaper/{username}")
    public List<Exam> getExamPaper(@PathVariable String username, @RequestBody String examName){

        System.out.println(examName);
        String name= examName.replace("\"","").replace("\"","");
        String OrganizationId= userTbRe.findOrgizationIdByUsername(username);
        Organization organization= organizationRe.findOrganizationById(OrganizationId);
        if(examName.equals("\"\"")){
            return examRe.findByOrganizationId(OrganizationId);
        }else {
            return examRe.findByOrganizationAndName(organization,name);
        }
    }
    @PostMapping("/updateExamPaper/{id}")
    public String updateExamPaper(@PathVariable Long id, @RequestBody Exam exam){
        Exam exam1=examRe.findById(id).get();
        exam1.setName(exam.getName());
        exam1.setDescription(exam.getDescription());
        exam1.setDuration(exam.getDuration());
        exam1.setTotalScore(exam.getTotalScore());
        examRe.save(exam1);
        return "success";
    }
    @DeleteMapping("/deleteExamPaper/{id}")
    public String deleteExamPaper(@PathVariable Long id) {
        examRe.deleteById(id);
        return "success";
    }
    @PostMapping("/generateExam")
    public String generateExam(@RequestBody ExamData examData){
        List<ExamQuestion> examQuestions=examService.generateExam(examData);
        System.out.println(examQuestions);
        return "success";

    }
}
